home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13096 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Forward declarations: why won't they work?
  5. Date: 23 Mar 1996 14:23:04 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4j11g8$140@clarknet.clark.net>
  8. References: <4ivpp0$iut@hustle.rahul.net>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. Theodore Sternberg (strnbrg@rahul.net) wrote:
  16. : Forward class declarations only seem to work sometimes.  When they don't 
  17. : work, I get a compiler error to the effect that "struct foo is an 
  18. : imcomplete type".  Can anyone tell me what's going on, i.e. when forward 
  19. : class declarations are and are not possible?
  20.  
  21. You'll get an error when the compiler would have to know, not just that 
  22. your struct is a struct, but what's IN the struct, in order to compile 
  23. your code.
  24.  
  25. For example,
  26.  
  27.     struct Foo;
  28.     struct Bar
  29.     {
  30.         Foo *f;
  31.     };
  32.  
  33. is fine. The compiler knows that Foo is a struct, and that f is therefore 
  34. a pointer-to-struct. The way a compiler sets aside space for a 
  35. pointer-to-struct is independent of the contents of the struct, so the 
  36. compiler is able to deal with the definition of Bar without having to 
  37. know what a Foo looks like. But,
  38.  
  39.     struct Foo;
  40.     struct Bar
  41.     {
  42.         Foo f;
  43.     };
  44.  
  45. won't work because now you're telling the compiler that a Foo itself, not 
  46. a pointer to it, is a member of Bar, and you are asking the computer to 
  47. set aside space for a Foo itself. To do this, the compiler _does_ have to 
  48. know what a Foo looks like. Since you haven't told it yet, you will get an 
  49. error.
  50.